var vrecode = null;
$(function(){
    window.onresize = function(){
        if(!isNull(vrecode)){
            changeSlide(vrecode);
        }
    };
    $("#slideTxtSelect ul li").click(function () {
        var index = $("#slideTxtSelect ul li").index($(this));
        $("#slideTxtSelect ul li").removeClass("active");
        $(this).addClass("active");
        $(".slide-content-div .slide-box-one").hide().eq(index).fadeIn();
    });
    //  加载历史记录
    loadHistory();
    //  轮播
    getSlideData();
    //  echartSize
    initBjTable();
    //  推荐功能
    tjgnList();
});
//   加载历史记录
function loadHistory() {
    $.ajax({
        url: "/portal/user/statistics/queryGnDjLb.do",
        data: {
            "rows": "6"
        },
        type: "post",
        timeout: sys_timeout,
        dataType: "json",
        async: false,
        error: function (XMLHttpRequest, textStatus) {
        },
        success: function (respText) {
            var list = respText.rows;
            var str = "";
            if(list.length>0){
                for(var i = 0;i<list.length;i++){
                    str+="<div class=\"history-one\" onclick=\"openHref('"+list[i].id+"','"+list[i].href+"','"+list[i].text+"','DATA_SY')\"><i class=\"fa fa-clock-o\"></i><span class=\"history-span\">"+list[i].text+"</span><span class=\"history-time\">"+list[i].sj+"</span></div>"
                }
                $(".history-scroll").html(str);
            }

        }
    });
}
//   收藏下拉框
function getSlideData() {
    var slideData = JSON.parse($("#zxdata").val()).sjcGnList;
    if(!isNull(slideData)){
        $(".slide-tab li").eq(1).show();
        $("#slideSelect").combobox({
            data:slideData,
            valueField:"SCJ_ID",
            textField:"SCJ_MC",
            onLoadSuccess: function () {
                var val = $(this).combobox("getData");
                $(this).combobox("select",val[0].SCJ_ID);
            },
            onSelect:function(record){
                changeSlide(record);
                vrecode = record;
            }
        });
        document.getElementById("_easyui_textbox_input2").readOnly=true;
    }else{
        $(".slide-tab li").eq(1).hide();
    }
}

//  推荐功能
function tjgnList(){
    var tjgnList = JSON.parse($("#tjgnList").val());
    if(!isNull(tjgnList)){
        $(".tjgn-div").height($("body").height()-48);
        var str = "";
        for(var i=0;i<tjgnList.length;i++){
            str+="<div class='gn-one' onclick=\"openHref('"+tjgnList[i].GN_ID+"','"+tjgnList[i].HREF+"','"+tjgnList[i].GN_MC+"','"+tjgnList[i].SYSCODE+"')\">\n" +
                '<div class="gn-icon">';
            if(isNull(tjgnList[i].JDYS)){
                str+='<i class="fa fa-address-book icon-blue"></i>';
            }else{
                str+='<i class="fa '+tjgnList[i].JDYS+'"></i>';
            }
            str+= '</div>\n' +
                '<div class="gn-title">'+tjgnList[i].GN_MC+'</div>\n' +
                '</div>'
        }
        $("#tjgn").html(str);
    }
}
//   改变轮播数据
function changeSlide(record) {
    var gnlist = record.gnList;
    $(".slideTxtBox").hide();
    $(".slideTxtBox .hd, .slideTxtBox .bd").empty();
    var bdStr="<ul>";
    var hdStr="<ul>";
    for (var v = 0; v < Math.ceil(gnlist.length/12);v++){
        bdStr+='<li>\n' +
            '<div class="slide-content">\n' +
            '<div class="gn-group">';
        for (var o=0;o<complay(gnlist.length,v);o++){
            bdStr+="<div class='gn-one' data-gnid='"+gnlist[o+v*12].GN_ID+"' onclick=\"openHref('"+gnlist[o+v*12].GN_ID+"','"+gnlist[o+v*12].HREF+"','"+gnlist[o+v*12].GN_MC+"','DATA_SY')\">\n" +
                '<div class="gn-icon">';
                if(isNull(gnlist[o+v*12].JDYS)) {
                    bdStr+='<i class="fa fa-address-book icon-blue"></i>'
                }else{
                    bdStr+='<i class="fa fa-'+gnlist[o+v*12].JDYS+'"></i>'
                }
            bdStr+='</div>\n' +
                '<div class="gn-title">'+gnlist[o+v*12].GN_MC+'</div>\n' +
                '</div>'
        }
        bdStr+='</div>\n' +
            '</div>\n' +
            '</li>';

        hdStr+='<li></li>';

    }
    bdStr+='</ul>';
    hdStr+='</ul>';
    $(".slideTxtBox .hd").html(hdStr);
    $(".slideTxtBox .bd").html(bdStr);
    homeSlide();
}
//  计算循环个数
function complay(number,v) {
    if (number - (v+1) * 12 >= 0) {
        var nu = 12;
    }else{
        var nu =  number - v*12;
    }
    return nu;
}
//   轮播
function homeSlide(){
    $(".slideTxtBox .tempWrap, .slideTxtBox ul li, .slideTxtBox ul li .slide-content").width($(".slideDiv").width()-3);
    $(".tjgn-div, #bgSlide, #bgSlide ul li, #bgSlide ul li .slide-content, .bgHistory .slide-box-one").height($("body").height()-48);
    $(".bgHistory .history-group").height($("body").height()-58);
    $(".bgHistory .history-scroll").height($("body").height()-78);
    jQuery(".slideTxtBox").slide({
        titCell:".hd ul",
        mainCell:".bd ul",
        autoPage:true,
        effect:"left",
        autoPlay:false
    });
    $(".slideTxtBox").fadeIn();
}

function initBjTable(){
    var mbList = JSON.parse($("#zxdata").val());
    function getWidth(cols){
        if(cols=="3"){
            return '33%'
        }
        if(cols=="4"){
            return '25%'
        }
    }

    function initTable(bjList,rownum,colnum){

        var table="";
        var thead="";
        var tobdy="";
        var width =getWidth(colnum)

        for(var i=0;i<colnum;i++){
            thead = thead + "<th width='"+width+"'></th>";
        }
        thead="<thead><tr>"+thead+"</tr></thead> \n";

        var tdId= 1;
        for(var i=0;i<rownum;i++){ //行
            tobdy=tobdy+"<tr> \n"
            for(var j=0;j<colnum;j++){ //列
                tobdy=tobdy+"<td id='tdId_"+tdId+"' height='310' ></td>";
                tdId++;
            }
            tobdy=tobdy+"</tr> \n";
        }
        tobdy= "<tbody id=\"tbody\" > \n"+tobdy+"</tbody> \n";
        table="<table width='100%' class='bjTable'  >\n"+thead+tobdy+"</table>";

        $("#bjMain").html(table);

        for(var i=0;i<bjList.length;i++){

            var bj= bjList[i];
            var cols= bj["COLS"];
            var dataArr=cols.split(",");//分割成字符串数组

            var begin = parseInt(dataArr[0]);
            var end = parseInt(dataArr[dataArr.length-1]);

            var minus = end-begin;
            var colspan = parseInt(minus%colnum) +1;
            var rowspan = parseInt(minus/colnum)  +1;

            for(var k=0;k<dataArr.length;k++){
                var m = dataArr[k];
                if(m!=begin){
                    $("#tdId_"+m).remove();
                }
            }
            var wzid =bj["WZID"];
            $("#tdId_"+begin).attr("colspan",colspan).attr("rowspan",rowspan);
            var tbyy_id=bj["TBYY_ID"];

            if(tbyy_id!=null){
                var h =  $("#tdId_"+begin).height()-50;
                var tbyy_mc = bj["TBYY_MC"];
                var mbid = bj["MBID"];
                var url = bj["TBYY_DZ"];
                var tbyy_href = bj["TBYY_HREF"];
                var str = '<div class="chartBox" id="wz_'+wzid+'"  wzid="'+wzid+'" cols="'+cols+'">\n'+
                    "<div class='gnTitle'><span class='title-s'>"+tbyy_mc+"</span>";
                if(!isNull(tbyy_href)){
                    str += "<div onclick=\"openHref('"+wzid+"','"+tbyy_href+"','"+tbyy_mc+"','DATA_PORTAL')\" class='chart-y'><i class='fa fa-list-ul'></i></div>"
                }
                str+="</div>\n"+
                    "<iframe width='100%' height='"+h+"' frameborder='0' style='background: #f8f8f8;' src='"+url+"'></iframe> \n"+
                    '</div>';
                $("#tdId_"+begin).html(str);
                // $("#tdId_"+begin).html(
                //     '<div class="chartBox" id="wz_'+wzid+'"  wzid="'+wzid+'" cols="'+cols+'">\n'+
                //     "<div class='gnTitle'><span class='title-s'>"+tbyy_mc+"</span><a href='"+tbyy_href+"' class='chart-y'><i class='fa fa-list-ul'></i></a></div>\n"+
                //     "<iframe width='100%' height='"+h+"' frameborder='0' style='background: #f8f8f8;' src='"+url+"'></iframe> \n"+
                //     '</div>'
                // );
            }
        }

    }
    if(!isNull(mbList.bjList)){
        initTable(mbList.bjList,mbList.rownum,mbList.colnum);
    }
}

function openHref(iframeid, iframehref, iframeName,type) {
    if(type != 'DATA_PORTAL'){
       var isOther = true
    }else{
       var isOther = false;
    }
    if(!isNull(iframehref)){
        parent.addIframe(iframeid, iframehref, iframeName,isOther);
    }
}


